package com.carkeeper.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carkeeper.dto.CompTypeDTO;
import com.carkeeper.dto.ParentDTO;
import com.carkeeper.exception.BussinessException;
import com.carkeeper.mapper.CompTypeMapper;
import com.carkeeper.pojo.CompTypePO;
import com.carkeeper.service.CompTypeService;
import com.carkeeper.vo.Pager;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CompTypeServiceImp extends ServiceImpl<CompTypeMapper, CompTypePO> implements CompTypeService {
    /*
     * 删除配件分类
     */
    @Override
    @Transactional
    public String deleteFirstCompTypeById(Long id) {
        if (id == null) {
            return "请输入id";
        }
        boolean success = removeById(id);
        if (!success) {
            return "id不存在";
        }
        return "删除成功";
    }

    /*
     * 新增配件分类
     */
    @Override
    @Transactional
    public String addFirstCompType(CompTypePO compType) {
        LambdaQueryWrapper<CompTypePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CompTypePO::getCode, compType.getCode());
        int count = count(lambdaQueryWrapper);
        if (count > 0) {
            throw new BussinessException("配件分类编码已存在！");
        }
        save(compType);
        return "新增成功";
    }

    /*
     * 根据id查询配件分类
     */
    @Override
    public CompTypeDTO queryCompTypeById(Long id) {
        CompTypePO compType = getById(id);
        if (compType == null) {
            throw new BussinessException("分类不存在");
        }
        CompTypeDTO compTypeDto = new CompTypeDTO();
        BeanUtils.copyProperties(compType, compTypeDto);
        if (compType.getParentId() != null && compType.getParentId() > 0) {
            CompTypePO parent = getById(compType.getParentId());
            ParentDTO parentDto = new ParentDTO();
            BeanUtils.copyProperties(parent, parentDto);
            compTypeDto.setParent(parentDto);
        }
        return compTypeDto;
    }

    /*
     * 获取配件分类分页数据
     */
    @Override
    public Pager<CompTypeDTO> getFirstCompTypeByPage(Long page, Long pagesize, Long parentId, String name, String code) {
        LambdaQueryChainWrapper<CompTypePO> lambdaQueryChainWrapper = lambdaQuery();
        if (parentId != null) {
            lambdaQueryChainWrapper.eq(CompTypePO::getParentId, parentId);
        } else {
            lambdaQueryChainWrapper.eq(CompTypePO::getParentId, 0);
        }
        if (!StringUtils.isEmpty(name)) {
            lambdaQueryChainWrapper.like(CompTypePO::getName, name);
        }
        if (!StringUtils.isEmpty(code)) {
            lambdaQueryChainWrapper.like(CompTypePO::getCode, code);
        }
        lambdaQueryChainWrapper.orderByDesc(CompTypePO::getCreateTime);
        Page<CompTypePO> result = lambdaQueryChainWrapper.page(new Page<>(page, pagesize));

        List<CompTypePO> list = result.getRecords();
        List<CompTypeDTO> compTypeDtos = new ArrayList<>();
        for (CompTypePO compType : list) {
            CompTypePO parent = getById(compType.getParentId());
            CompTypeDTO compTypeDto = new CompTypeDTO();
            if (parent != null) {
                ParentDTO parentDto = new ParentDTO();
                BeanUtils.copyProperties(parent, parentDto);
                compTypeDto.setParent(parentDto);
            }
            BeanUtils.copyProperties(compType, compTypeDto);
            compTypeDtos.add(compTypeDto);
        }

        Page<CompTypeDTO> page1 = new Page<>();
        page1.setPages(page);
        page1.setSize(pagesize);
        page1.setRecords(compTypeDtos);
        long counts = result.getTotal();
        long pages = result.getPages();
        Pager<CompTypeDTO> pager = new Pager<>(page1);
        pager.setCounts(counts);
        pager.setPages(pages);
        return pager;
    }

    /*
     * 更新配件分类
     */
    @Override
    @Transactional
    public String updateFirstCompType(Long id, CompTypePO compType) {
        CompTypePO source = getById(id);
        if (source == null) {
            throw new BussinessException("修改的对象不存在！");
        }
        LambdaQueryWrapper<CompTypePO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(CompTypePO::getCode, compType.getCode());
        int count = count(lambdaQueryWrapper);
        boolean isSelf = compType.getCode() == null ? true : compType.getCode().equals(source.getCode());

        if (!isSelf && count > 0) {
            throw new BussinessException("配件分类编码已存在！");
        }
        CompTypePO sourceComType = getById(id);
        if (sourceComType == null) {
            throw new BussinessException("分类不存在！");
        }
        // 如果有，则修改
        updateById(compType);
        return "更新分类成功";
    }
}
